///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_ADAPTORS_H_
#define _BUOLA_IMAGE_ADAPTORS_H_

#include <buola/image.h>
#include <buola/image/cmview.h>
#include <buola/image/traits.h>
#include <buola/image/cimage.h>
#include <array>

namespace buola { namespace img {

///Gets a view to part of another view

template<typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<>::type>::type
sub_view(const tView &pView,const CPoint_i &pP,const CSize_i &pS)
{
    typedef typename tView::template MRebind<>::type tResult;
    return tResult(pView.RawData(pP.x,pP.y),pS,pView.Stride());
}

///Gets a view to part of another view

template<typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<>::type>::type
sub_view(const tView &pView,const CPoint_i &pP)
{
    typedef typename tView::template MRebind<>::type tResult;
    return tResult(pView.RawData(pP.x,pP.y),{pView.Width()-pP.x,pView.Height()-pP.y},pView.Stride());
}

///Gets a view to part of another view

template<typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<>::type>::type
sub_view(const tView &pView,const CRect_i &pR)
{
    typedef typename tView::template MRebind<>::type tResult;
    return tResult(pView.RawData(pR.l,pR.t),pR.Size(),pView.Stride());
}

///Gets a view to a row of another view

template<typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<>::type>::type
row_view(const tView &pView,int y)
{
    return sub_view(pView,{0,y},{pView.Width(),1});
}

template<typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<>::type>::type
col_view(const tView &pView,int x)
{
    return sub_view(pView,{x,0},{1,pView.Height()});
}

///Gets a view that is a vertical flip of another view

template<typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<>::type>::type
vflip_view(const tView &pView)
{
    typedef typename tView::template MRebind<>::type tResult;
    return tResult(pView.RawData(0,pView.Height()-1),pView.Size(),-pView.Stride());
}

///Gets a view that reinterprets the pixels as a different format
///
///Both views must have the same pixel size

template<typename tFmt,typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<tFmt>::type>::type
reinterpret_view(const tView &pView)
{
    typedef typename tView::template MRebind<tFmt>::type tResult;
    
    static_assert(tView::TFormat::mPixelSize==tResult::TFormat::mPixelSize,
                  "source and destination format for reinterpret_view must have the same pixel size");
    
    return tResult(reinterpret_cast<typename tResult::TPointer>(pView.RawData()),pView.Size(),pView.Stride());
}

///Gets a view to a channel of another view
///
///Both views must have the same pixel size

template<typename tFmt,typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<tFmt,tView::TFormat::TColorSpace::cChannels>::type>::type
channel_view(const tView &pView,int pChannel)
{
    typedef typename tView::template MRebind<tFmt,tView::TFormat::TColorSpace::cChannels>::type tResult;
    
    static_assert(MSameChannel<tView,tResult>::value,
                  "source and destination format for channel_view must have the same channel type");
    
    return tResult(pView.RawData()+pChannel,pView.Size(),pView.Stride());
}

///Gets a view with the same pixels in a different shape
///
///The following restrictions apply:
///   - the source view must be continuous (i.e. the pixels must be packed)
///   - the destination view must not have more pixels than the source

template<typename tView>
typename std::enable_if<MIsView<tView>::value,typename tView::template MRebind<>::type>::type
reshape_view(const tView &pView,const CSize_i &pSize)
{
    typedef typename tView::template MRebind<>::type tResult;
    assert(pView.Continuous());
    assert(pSize.x*pSize.y<=pView.Size().x*pView.Size().y);

    return tResult(pView.RawData(),pSize);
}

/*namespace image*/ } /*namespace buola*/ }

#endif
